package doantotnghiep.daoimpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.business.CategoryBusiness;
import doantotnghiep.dao.MedicamentDao;
import doantotnghiep.model.Category;
import doantotnghiep.model.Medicament;

public class MedicamentDaoImpl implements MedicamentDao {

	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	@Autowired 
	private CategoryBusiness categoryBusiness;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medicament> listMedicament() {
		List<Medicament> list = this.hibernateTemplate.find("from Medicament where status = 0 order by id");
		return list;
	}
	
	@Override
	public void deleteMedicament(int id) {
		Medicament entity = this.findMedicamentByID(id);
		if (entity != null){
			entity.setStatus(1);
			this.hibernateTemplate.saveOrUpdate(entity);
		}
	}

	@Override
	public void saveMedicament(Medicament medicament) {
		this.hibernateTemplate.saveOrUpdate(medicament);		
	}

	@Override
	public void addMedicament(Medicament medicament) {
		int idCategory = medicament.getCategory().getId(); 
		Category category = categoryBusiness.getCategoryrById(idCategory);
		this.hibernateTemplate.saveOrUpdate(medicament);
        
		//increas number of medicine of category
		category.setNumberMedicine(category.getNumberMedicine()+1);
		
		hibernateTemplate.saveOrUpdate(category);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Medicament findMedicamentByID(int id) {
		List<Medicament> medicaments = hibernateTemplate.find("from Medicament where status = 0 and id ="+ id);
		if(medicaments!=null){
			return medicaments.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medicament> getMedicamentByKey(String key) {
		List<Medicament> list = hibernateTemplate.find("from Medicament where TrangThai = 0 and medicamentName like '%"+key+"%'");
		System.out.println("from Medicament where TrangThai = 0 and medicamentName like '%"+key+"%'");
		return list;
	}

	@Override
	public void increaseNumberOfBatchofMedicine(int medicamentID,int i) {
		Medicament medicament = this.findMedicamentByID(medicamentID);
		if(medicament!=null){
			medicament.setTotalOfBatch(medicament.getTotalOfBatch()+i);
			hibernateTemplate.saveOrUpdate(medicament);
		}	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medicament> getAllMedicamentByKeyWord(String key) {
		List<Medicament> list = hibernateTemplate.find("from Medicament where TrangThai = 0 and TongSoLo > 0 and medicamentName like '%"+key+"%'");
		return list;
	}

	@Override
	public void decreaseNumberofBatch(int medicament_ID, int totalBatch) {
		Medicament medicament = this.findMedicamentByID(medicament_ID);
		if(medicament!=null){
			medicament.setTotalOfBatch(medicament.getTotalOfBatch()-totalBatch);
			hibernateTemplate.saveOrUpdate(medicament);
		}
		
	}

}
